<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'inspectorder.add'" ></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'inspectorder.delete'" ></el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search.test_code" placeholder="检验单号" clearable @keyup.enter="upsearch"></el-input>
                    <el-input v-model="search.purchase_code" placeholder="入库单号" clearable @keyup.enter="upsearch"></el-input>
                    <el-input v-model="search.goods_name" placeholder="品名" clearable @keyup.enter="upsearch"></el-input>
                    <el-select v-model="search.apply_id" placeholder="供应商" filterable clearable>
                        <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                    </el-select>
                    <el-date-picker v-model="search.date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <el-select v-model="search.is_complate" placeholder="完成状态" clearable>
                        <el-option label="未完" :value="0"></el-option>
                        <el-option label="已完成" :value="1"></el-option>
                        <el-option label="全部" :value="10"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" border :params="search" tableName="inspectTable" :column="inspectColumn">
                <el-table-column type="selection" width="50" align="center"></el-table-column>
                <template #test_code="{row}">
                    <div class="flex">
                        <el-link :underline="false" type="primary" @click="table_show(row)">{{ row.test_code }}</el-link>
                        <el-icon class="icon" :size="20" @click="common.copy(row.test_code)"><el-icon-Edit /></el-icon>
                        <el-icon class="icon" :size="20" color="#29f" @click="test_bell(row)" v-auth="'inspectorder.message'"><el-icon-BellFilled /></el-icon>
                    </div>
                </template>
                <template #work_name="{row}">
                    <span v-for="(item, index) in row.work_uids_name" :key="item.id">
                        {{item.name}}
                        <span v-if="index < row.work_uids_name.length - 1">/</span>
                    </span>
                </template>
                <template #progress="{row}">
                    <div class="progress" @click="show_print(row)">
                        <div style="margin-bottom: 5px">{{row.use_number}} / {{row.all_number}}</div>
                        <el-progress style="width: 100%" :percentage="Math.round((row.use_number / row.all_number) * 100)" :show-text="false" />
                    </div>
                </template>
                <template #purchase_ware_code="{row}">
                    <el-tag type="info" v-for="(item,index) in row.purchase_ware_code" :key="index" style="margin-right: 20px" @click="table_show_house(item)">
                        {{ item.purchase_ware_code }}
                    </el-tag>
                </template>
                <template #operation="scope">
                    <div class="buttonFlex">
                        <el-popconfirm :title="`确定${scope.row.is_complate == 1 ? '未完' : '完成'}吗？`" @confirm="table_complate(scope.row)">
                            <template #reference>
                                <el-button size="small" v-auth="'inspectorder.complate'" v-if="scope.row.use_number != 0">{{scope.row.is_complate == 1 ? '已完成' : '未完'}}</el-button>
                            </template>
                        </el-popconfirm>
                        <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'inspectorder.edit'">编辑</el-button>
                        <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                            <template #reference>
                                <el-button type="danger" size="small" v-auth="'inspectorder.delete'" v-if="scope.row.use_number == 0">删除</el-button>
                            </template>
                        </el-popconfirm>
                        <el-button type="primary" size="small" @click="table_select_house(scope.row)" v-auth="'inspectorder.print'">打印</el-button>
                    </div>
                </template>



                <!-- <el-table-column label="检验单号" prop="test_code" width="230" align="center">
                    <template #default="{row}">
                        <div class="flex">
                            <el-link type="primary" @click="table_show(row)">{{ row.test_code }}</el-link>
                            <el-icon class="icon" :size="20" @click="common.copy(row.test_code)"><el-icon-Edit /></el-icon>
                            <el-icon class="icon" :size="20" color="#29f" @click="test_bell(row)" v-auth="'inspectorder.message'"><el-icon-BellFilled /></el-icon>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="检验进度" align="center" width="150">
                    <template #default="{row}">
                        <div class="progress" @click="show_print(row)">
                            <div style="margin-bottom: 5px">{{row.use_number}} / {{row.all_number}}</div>
                            <el-progress style="width: 100%" :percentage="Math.round((row.use_number / row.all_number) * 100)" :show-text="false" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="入库单" prop="remark" align="center">
                    <template #default="{row}">
                        <el-tag type="info" v-for="(item,index) in row.purchase_ware_code" :key="index" style="margin-right: 20px" @click="table_show_house(item)">
                            {{ item.purchase_ware_code }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="品名" prop="goods_name" align="center" width="120"></el-table-column>
                <el-table-column label="供应商" prop="apply.apply_name" align="center" width="120"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center" width="120"></el-table-column>
                <el-table-column label="业务员" prop="createstaff.name" align="center" width="120"></el-table-column>
                <el-table-column label="日期" prop="test_date" align="center" width="120"></el-table-column>
                <el-table-column label="操作" align="center" width="230">
                    <template #default="scope">
                        <div class="buttonFlex">
                            <el-popconfirm :title="`确定${scope.row.is_complate == 1 ? '未完' : '完成'}吗？`" @confirm="table_complate(scope.row)">
                                <template #reference>
                                    <el-button size="small" v-auth="'inspectorder.complate'" v-if="scope.row.use_number != 0">{{scope.row.is_complate == 1 ? '已完成' : '未完'}}</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'inspectorder.edit'">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="danger" size="small" v-auth="'inspectorder.delete'" v-if="scope.row.use_number == 0">删除</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button type="primary" size="small" @click="table_select_house(scope.row)" v-auth="'inspectorder.print'">打印</el-button>
                        </div>
                    </template>
                </el-table-column> -->
            </scTable>
        </el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>

    <purchasehouse-dialog v-if="dialog.purchasehouse" ref="purchasehouseDialog" @success="handleSuccess" @closed="dialog.purchasehouse = false"></purchasehouse-dialog>
    <dyehouse-dialog v-if="dialog.dyehouse" ref="dyehouseDialog" @success="handleSuccess" @closed="dialog.dye = false"></dyehouse-dialog>
    <processhouse-dialog v-if="dialog.processhouse" ref="processhouseDialog" @success="handleSuccess" @closed="dialog.process = false"></processhouse-dialog>
    <notice-save v-if="dialog.notice" ref="noticeDialog" @success="handleSuccess" @closed="dialog.notice = false"></notice-save>

    
    <select-house v-if="dialog.house" ref="houseDialog" @success="houseBack" @closed="dialog.house = false" :query="{test_code: test_code}"></select-house>
    <test-print v-if="dialog.test" ref="testDialog" @success="handleSuccess" @closed="dialog.test = false"></test-print>
</template>

<script>
import saveDialog from './save'
import selectHouse from './selectHouse'



import purchasehouseDialog from '@/views/business/purchase/arrival/save.vue'
import dyehouseDialog from '@/views/business/dye/house/save.vue'
import processhouseDialog from '@/views/business/machining/house/save.vue'
import NoticeSave from '../../noticeSave.vue'
import testPrint from './testPrint.vue'
export default {
    name: '采购计划管理',
    components: {
        saveDialog, purchasehouseDialog, dyehouseDialog, processhouseDialog, NoticeSave, selectHouse, testPrint
    },
    data() {
        return {
            dialog: {
                save: false,
                purchasehouse: false,
                dyehouse: false,
                processhouse: false,
                notice: false,
                test: false,
                house: false,
            },
            apiObj: this.$API.business.inspect.order.list,
            selection: [],
            search: {
                purchase_code: null,
                date: null,
                status: null,
                goods_name: null,
                test_code: null,
                is_complate: 0,
            },
            applyList: [],
            test_id: "",
            test_code: "",
            goods_name: "",
            apply_name: "",
            dye_factory_name: "",
            process_factory_name: "",
            inspectColumn: [
                { label:"检验单号", prop: "test_code", width: "230"},
                { label:"检验进度", prop: "progress", width: "150"},
                { label:"检验员", prop: "work_name", width: "150"},
                { label:"入库单", prop: "purchase_ware_code"},
                { label:"品名", prop: "goods_name", width: "120"},
                { label:"供应商", prop: "apply.apply_name", width: "120"},
                { label:"备注", prop: "remark", width: "120"},
                { label:"业务员", prop: "createstaff.name", width: "120"},
                { label:"日期", prop: "test_date", width: "120"},
                { label: "操作", prop: "operation", width: "230",fixed: 'right'},
            ]
        }
    },
    mounted() {
		this.getApply();
    },
    methods: {
        //添加
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('show').setData(row)
            })
            // this.$refs.table.toggleRowExpansion(row)
        },
        // 完成状态切换
        async table_complate(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.inspect.order.complate.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("操作成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //删除
        async table_del(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.inspect.order.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        async table_del_all(delete_ids) {
            var reqData = { id: delete_ids }
            var res = await this.$API.business.inspect.order.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //批量删除
        async batch_del() {
            var delete_ids = [];
            this.selection.forEach((item) => {
                delete_ids.push(item.id)
            })

            this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                type: 'warning'
            }).then(() => {
                this.table_del_all(delete_ids)
            }).catch(() => {

            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess(data, mode) {
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            } else if (mode == 'purchase') {
                this.$refs.table.refresh()
            } else if (mode == 'copy') {
                this.$refs.table.refresh()
            }
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        // 供应商列表
		async getApply() {
			var res = await this.$API.basic.apply.all.get({type: 1});
			this.applyList = res.data
		},
        // 采购提醒
        test_bell(row){
            let data = {
                id: row.id,
                code: row.test_code,
                apply_id: row.apply_id
            }
            this.dialog.notice = true
            this.$nextTick(() => {
                this.$refs.noticeDialog.open('edit').setData(data,'检验单',5)
            })
        },
        // 查看入库单
        table_show_house(row){
            this.dialog[row.input_type == 1 ? 'purchasehouse' : row.input_type == 2 ? 'dyehouse' : 'processhouse'] = true
            this.$nextTick(() => {
                this.$refs[row.input_type == 1 ? 'purchasehouseDialog' : row.input_type == 2 ? 'dyehouseDialog' : 'processhouseDialog'].open('show').setData(row)
            })
        },

        table_select_house(row){
            this.test_id = row.id
            this.test_code = row.test_code
            this.goods_name = row.goods_name
            this.apply_name = row.apply?.apply_name
            this.dye_factory_name = row.dye_factory_name
            this.process_factory_name = row.process_factory_name
            this.dialog.house = true
            this.$nextTick(() => {
                this.$refs.houseDialog.open('add')
            })
        },
        houseBack(data){
            let array = []
            data.forEach(item => {
                array.push(item.id)
            })
            let form = {
                id: array,
                test_id: this.test_id,
                test_code: this.test_code,
                goods_name: this.goods_name,
                apply_name: this.apply_name,
                dye_factory_name: this.dye_factory_name,
                process_factory_name: this.process_factory_name,
            }
            this.dialog.test = true
            this.$nextTick(() => {
                this.$refs.testDialog.open('edit').setData(form)
            })
        },
        show_print(row){
            console.log(row,15154)
            let array = []
            row.purchase_ware_code.forEach(item => {
                array.push(item.id)
            })
            let form = {
                id: array,
                test_id: row.id,
                test_code: row.test_code,
                goods_name: row.goods_name,
                apply_name: row.apply?.apply_name,
                dye_factory_name: row.dye_factory_name,
                process_factory_name: row.process_factory_name,
            }
            this.dialog.test = true
            this.$nextTick(() => {
                this.$refs.testDialog.open('edit').setData(form)
            })
        },
    }
}
</script>

<style lang="scss">
.margin{
    margin: 5px 0;
}
.flex {
    display: flex;
    align-items: center;
    justify-content: center;
    .icon{
        cursor: pointer;
        margin-left: 5px;
    }
}
.progress{
    width: 100%;
    display: flex;
    flex-direction: column;
}
</style>
